package org.yl.db_core.base;

import org.yl.db_core.utils.BaseUtil;
import org.yl.db_core.utils.constant.SqlConstant;
import org.yl.db_core.utils.db.CreateMysqlSqlUtil;
import org.yl.db_core.utils.db.ExecuteJdbcSql;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: jdbcTemplate
 * @Author: YL
 * @CreateDate: 2018/11/10 14:00
 * @Version: 1.0
 */
@Repository
@Configuration
public class BaseDao {

    private final static Logger logger = LoggerFactory.getLogger(BaseDao.class);

    @Autowired
    @Qualifier(value = "primaryJdbcTemplate")
    private JdbcTemplate primaryJdbcTemplate;

    @Autowired
    private CreateMysqlSqlUtil sqlUtil;

    @Autowired
    private ExecuteJdbcSql jdbcSql;


    /**
     * 查询 对象List
     */
    public List queryByList(Object object, String sqlType, BeanPropertyRowMapper mapper, Integer page, Integer row) {
        Map map = sqlUtil.createQuerySQL(object, sqlType, new ArrayList(), page, row);
        String sql = String.valueOf(map.get("sql"));
        Object[] objects = (Object[]) map.get("objects");
        logger.info("查询 对象List,  {}", BaseUtil.toJSON(map));
        List list = primaryJdbcTemplate.query(sql, objects, mapper);
        if (list != null && list.size() > 0) {
            return list;
        } else {
            return new ArrayList();
        }
    }

    /**
     * 查询 对象ListCount
     */
    public Integer queryByListCount(Object object, String sqlType, BeanPropertyRowMapper mapper) {
        Map map = sqlUtil.createQuerySQL(object, sqlType, new ArrayList(), null, null);
        String sql = String.valueOf(map.get("sql"));
        Object[] objects = (Object[]) map.get("objects");
        logger.info("查询 对象ListCount,  {}", BaseUtil.toJSON(map));
        List list = primaryJdbcTemplate.query(sql, objects, mapper);
        if (list != null && list.size() > 0) {
            return list.size();
        } else {
            return 0;
        }
    }

    /**
     * 查询 对象List
     */
    public List queryByList(String sql, Object[] objects, BeanPropertyRowMapper mapper) {
        logger.info("查询 对象List, {}", sql);
        List list = primaryJdbcTemplate.query(sql, objects, mapper);
        if (list != null && list.size() > 0) {
            return list;
        } else {
            return new ArrayList();
        }
    }

    /**
     * 查询 对象List
     */
    public List queryByList(Object object, String sqlType, Integer page, Integer row) {
        Map map = sqlUtil.createQuerySQL(object, sqlType, new ArrayList(), page, row);
        String sql = String.valueOf(map.get("sql"));
        logger.info("查询 对象List, {}", sql);
        List list = jdbcSql.queryForList(sql, object);
        if (list != null && list.size() > 0) {
            return list;
        } else {
            return new ArrayList();
        }
    }

    /**
     * 查询 单个对象
     */
    public BaseModel queryById(Object object, String sqlType, BeanPropertyRowMapper mapper) {
        Map map = sqlUtil.createQuerySQL(object, sqlType, new ArrayList(), null, null);
        String sql = String.valueOf(map.get("sql"));
        Object[] objects = (Object[]) map.get("objects");
        logger.info("查询 单个对象, {}", BaseUtil.toJSON(map));
        List list = primaryJdbcTemplate.query(sql, objects, mapper);
        if (list != null && list.size() > 0) {
            BaseModel model = (BaseModel) list.get(0);
            return model;
        } else {
            return null;
        }
    }

    /**
     * 查询 单个对象
     */
    public BaseModel queryById(String sql, Object[] objects, BeanPropertyRowMapper mapper) {
        logger.info("查询 单个对象, {}", sql);
        List list = primaryJdbcTemplate.query(sql, objects, mapper);
        if (list != null && list.size() > 0) {
            BaseModel model = (BaseModel) list.get(0);
            return model;
        } else {
            return null;
        }
    }

    /**
     * 查询 单个对象
     */
    public BaseModel queryById(Object object, String sqlType) {
        Map map = sqlUtil.createQuerySQL(object, sqlType, new ArrayList(), null, null);
        String sql = String.valueOf(map.get("sql"));
        logger.info("查询 单个对象, {}", sql);
        List list = jdbcSql.queryForList(sql, object);
        if (list != null && list.size() > 0) {
            BaseModel model = (BaseModel) list.get(0);
            return model;
        } else {
            return null;
        }
    }

    /**
     * 查询 单个数据
     */
    public Object queryByObject(Object object, String sqlType) {
        Map map = sqlUtil.createQuerySQL(object, sqlType, new ArrayList(), null, null);
        String sql = String.valueOf(map.get("sql"));
        Object[] objects = (Object[]) map.get("objects");
        logger.info("查询 单个数据, {}", BaseUtil.toJSON(map));
        Object object1 = primaryJdbcTemplate.query(sql, objects, new BeanPropertyRowMapper(Object.class));
        if (object1 != null) {
            return object1;
        } else {
            return null;
        }
    }

    /**
     * 查询 单个数据
     */
    public Object queryByObject(String sql, Object[] objects) {
        logger.info("查询 单个数据, {}", sql);
        Object object1 = primaryJdbcTemplate.query(sql, objects, new BeanPropertyRowMapper(Object.class));
        if (object1 != null) {
            return object1;
        } else {
            return null;
        }
    }

    /**
     * 更新
     */
    public int saveOrUpdateOrDelete(Object object, String sqlType) {
        Map map = sqlUtil.createQuerySQL(object, sqlType, new ArrayList(), null, null);
        String sql = String.valueOf(map.get("sql"));
        Object[] objects = (Object[]) map.get("objects");
        logger.info("更新数据, {}", BaseUtil.toJSON(map));
        return primaryJdbcTemplate.update(sql, objects);
    }

    /**
     * 更新
     */
    public int saveOrUpdateOrDeleteforJdbc(Object object, String sqlType) {
        Map map = sqlUtil.createQuerySQL(object, sqlType, new ArrayList(), null, null);
        String sql = String.valueOf(map.get("sql"));
        logger.info("更新数据, {}", BaseUtil.toJSON(map));
        return jdbcSql.insertOrUpdateObject(sql, object, sqlType);
    }

    /**
     * 批量更新
     */
    public int batchSaveOrUpdate(Object object, List list) {
        Map map = sqlUtil.createQuerySQL(object, SqlConstant.BATCHINSERT, list, null, null);
        String sql = String.valueOf(map.get("sql"));
        List<Object[]>  objects = (List<Object[]> ) map.get("objects");
        logger.info("批量更新数据, {}", BaseUtil.toJSON(map));
        primaryJdbcTemplate.batchUpdate(sql, objects);
        return 1;
    }

    /**
     * 更新
     */
    public int saveOrUpdateOrDelete(String sql, Object[] objects) {
        logger.info("更新数据, {}", sql);
        return primaryJdbcTemplate.update(sql, objects);
    }


}
